const express = require('express')
const multer = require('multer')
var upload = multer({ dest: 'uploads/' })
const fs = require('fs')
const handler = require('./router-handler')

const router = express.Router()

var createFolder = function(folder) {
    try {
        fs.accessSync(folder);
    } catch (e) {
        fs.mkdirSync(folder);
    }
}

var uploadFolder = './uploads/'; //文件上传位置
createFolder(uploadFolder);
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, uploadFolder)
    },
    filename: function(req, file, cb) {
        cb(null, file.originalname)

    }

})

var upload = multer({ storage: storage })

router.post('/uploads', upload.single('file'), upload)

router.get('/getbooks', handler.getbooks);

router.post('/delbook', handler.delbook);

router.post('/addbook', handler.addbook);

router.post('/querybook', handler.querybook);

router.post('/editbook', handler.editbook);

router.get('/alldelbook', handler.alldelbook);

router.get('/getBookByParams', handler.getBookByParams);

router.get('/userMsg', handler.userMsg);

router.get('/getKeywords', handler.getKeywords);

module.exports = router;